package cn.tsa.share.jdbc.controller;

import cn.tsa.share.jdbc.domain.Order;
import cn.tsa.share.jdbc.domain.OrderQuery;
import cn.tsa.share.jdbc.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;
import java.util.stream.IntStream;

/**
 * @author hongwei
 */
@RestController
@Slf4j
@RequestMapping("order")
public class OrderTestController {

    private final OrderService orderService;

    public OrderTestController(OrderService orderService) {
        this.orderService = orderService;
    }

    @GetMapping("/add/{loopIndex}")
    public Object add(@PathVariable Integer loopIndex) {
        for (int i = 0; i < loopIndex; i++) {
            Order order = new Order();
            order.setUserId(i);
            Random random = new Random();
            order.setOrderType(String.valueOf(random.nextInt(3)));
            order.setStatusType(String.valueOf(random.nextInt(3)));
            Long result = orderService.addOrder(order);
            log.info("insert:" + order + ",result=" + result);
        }
        return "ok";
    }

    @GetMapping("/add/mix/{loopIndex}")
    public Object addMix(@PathVariable Integer loopIndex) {
        for (int i = 0; i < loopIndex; i++) {
            Order order = new Order();
            order.setUserId(i);
            Long result = orderService.addOrder(order);
            log.info("insert:" + order + ",result=" + result);
        }
        return "ok";
    }

    @PostMapping("search")
    public List<Order> search(@RequestBody OrderQuery orderQuery) {
        return orderService.searchAllList(orderQuery);
    }
}
